class SeriesController < ApplicationController
  before_filter :find_series,   :only => [:show, :edit, :update, :destroy]
  before_filter :require_login, :only => [:create, :edit, :new, :update, :destroy]
  
  # GET, one_series_url
  def index
    @series = Series.find :all
  end
  
  # GET, one_series_url
  def show
     @events_series = @series.events
  end
  
  # GET, new_series_url
  def new
    @series = Series.new
  end
  
  # POST, one_series_url
  def create
    @series = Series.new(params[:series])
    @series.save!
    redirect_to one_series_url(@series)
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Nice one, jackass.'
    render :action => 'new'
  end
  
  # GET, edit_series_url
  def edit
    redirect_to one_series_url if @series.end_time < Time.now 
  end
  
  # PUT, one_series_url
  def update
    @series.update_attributes!(params[:series])
    redirect_to one_series_url(@series)
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Nice one, jackass'
    render :action => 'edit'
  end
  
  # DELETE, one_series_url
  def destroy
    @series.destroy
    redirect_to series_url
  end
  
  def find_series
    @series = Series.find params[:id]
  end
  
protected
  def require_login
    if !logged_in?
      flash[:error] = 'You must be logged in to do that'
      redirect_to new_session_url
    end
  end
end


